import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
import ElementPlus from 'element-plus'
import router from './router'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import { createPinia } from 'pinia';
import { useMainStore } from '../src/store/index.js';
// import { isPermission } from './hook/permission.js';
import permissionDirective from './hook/permission.js';

import "./api/mock.js";
import api from './api/api'

router.beforeEach((to, form, next) => {
    const mainStore = useMainStore()
    if (to.name != 'login') {
        mainStore.getToken()
        mainStore.getPermission()
    }
    const token = mainStore.token
    if (!token && to.name !== 'login') {
        console.log('跳回首页了');
        next({ name: 'login' })
    }
    else {
        next()
    }
})


const app = createApp(App)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
app.directive('permission', permissionDirective);
// isPermission(app)
app.config.globalProperties.$api = api
app.use(ElementPlus)
app.use(router)
app.use(createPinia());
app.mount('#app')
